using System.Collections;
using I2.Loc;
using UnityEngine;

namespace BCWAccountVip
{
	public class AccountVipShowTime : MonoBehaviour
	{
		private UILabel _label;

		private Coroutine _timeCoriutine;

		private void Awake()
		{
			_label = GetComponent<UILabel>();
			_label.text = ScriptLocalization.Get("Game_Common/Premium_btn_label1");
		}

		private void OnEnable()
		{
			if (_label != null)
			{
				SetTime();
				ManagerVip.changeVipTime += SetTime;
			}
		}

		private void OnDisable()
		{
			if (_label != null)
			{
				ManagerVip.changeVipTime -= SetTime;
			}
		}

		public void SetTime()
		{
			if (_timeCoriutine != null)
			{
				StopCoroutine(_timeCoriutine);
				_timeCoriutine = null;
			}
			_timeCoriutine = StartCoroutine(SetTimeCoroutine());
		}

		private IEnumerator SetTimeCoroutine()
		{
			float secInHour = 3600f;
			float secInDay = secInHour * 24f;
			float secInMinutes = 60f;
			if (ManagerVip.GetInstance().IsCheeter)
			{
				_label.text = "no internet";
			}
			else
			{
				if (!ManagerVip.GetInstance().HasVip())
				{
					yield break;
				}
				while (true)
				{
					int seconds = ManagerVip.GetInstance().GetSeconds();
					if ((float)seconds > secInDay)
					{
						_label.text = Mathf.Ceil((float)seconds / secInDay) + " " + ScriptLocalization.Get("Game_Common/Premium_days_label");
						yield return new WaitForSeconds((float)seconds - Mathf.Floor((float)seconds / secInDay) * secInDay + 1f);
						continue;
					}
					if ((float)seconds > secInHour)
					{
						_label.text = Mathf.Ceil((float)seconds / secInHour) + " " + ScriptLocalization.Get("Game_Common/Premium_hour_label");
						yield return new WaitForSeconds((float)seconds - Mathf.Floor((float)seconds / secInHour) * secInHour + 1f);
						continue;
					}
					if (seconds <= 0)
					{
						break;
					}
					float minutes = Mathf.Floor((float)seconds / secInMinutes);
					_label.text = string.Empty;
					if (minutes > 0f)
					{
						UILabel label = _label;
						string text = label.text;
						label.text = text + minutes + " " + ScriptLocalization.Get("Game_Common/Premium_min_label") + " ";
					}
					UILabel label2 = _label;
					label2.text = label2.text + ((float)seconds - minutes * secInMinutes) + " " + ScriptLocalization.Get("Game_Common/Premium_sec_label");
					yield return new WaitForSeconds(1f);
				}
				_label.text = ScriptLocalization.Get("Game_Common/Premium_btn_label1");
				if (AccountVipUI.instance != null)
				{
					AccountVipUI.instance.RemindAccountVipWindow();
				}
			}
		}
	}
}
